package org.jeecg.qis.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.qis.entity.QisMatdetailValue;
import org.jeecg.qis.entity.QisMatheadValue;
import org.jeecg.qis.service.IQisMatdetailValueService;
import org.jeecg.qis.service.IQisMatheadValueService;
import org.jeecg.qis.vo.QisMatheadValueVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: qis-来料检测送检单数据表
 * @Author: shaolong
 * @Date: 2022-10-09
 * @Version: V1.0
 */
@Api(tags = "qis-来料检测送检单数据表")
@RestController
@RequestMapping("/qis/qisMatheadValue")
@Slf4j
public class QisMatheadValueController extends JeecgController<QisMatheadValue, IQisMatheadValueService> {

	@Autowired
	private IQisMatheadValueService qisMatheadValueService;

	@Autowired
	private IQisMatdetailValueService qisMatdetailValueService;



	/*---------------------------------主表处理-begin-------------------------------------*/

	/**
	 * 分页列表查询  erp
	 *
	 * @param qisMatheadValue
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "qis-来料检测送检单数据表-分页列表查询")
	@ApiOperation(value = "qis-来料检测送检单数据表-分页列表查询", notes = "qis-来料检测送检单数据表-分页列表查询")
	@GetMapping(value = "/list")
	@DS("#header.his")
	public Result<IPage<QisMatheadValueVO>> queryPageList(QisMatheadValue qisMatheadValue,
														  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
														  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
														  HttpServletRequest req) {
		Page<QisMatheadValueVO> page = new Page<QisMatheadValueVO>(pageNo, pageSize);
		IPage<QisMatheadValueVO> pageList = qisMatheadValueService.queryList(page, qisMatheadValue);
		return Result.OK(pageList);
	}

	@ApiOperation(value = "来料检验-生成检验单", notes = "来料检验-生成检验单")
	@PostMapping(value = "/createCheckSheet")
	@DS("#header.line")
	public Result<T> createCheckSheet(@RequestBody QisMatheadValue qisMatheadValue,
									  HttpServletRequest req) {
		qisMatheadValueService.createCheckSheet(qisMatheadValue);
		return Result.OK("生成检验单成功！");
	}

	@ApiOperation(value = "qis-来料检测送检单数据表-分页列表查询", notes = "qis-来料检测送检单数据表-分页列表查询")
	@GetMapping(value = "/inspectionItems")
	@DS("#header.his")
	public Result<Map<String, Object>> getInspectionItems(QisMatheadValue qisMatheadValue,
														  HttpServletRequest req) {
		Map<String, Object> map = qisMatheadValueService.getInspectionItems(qisMatheadValue);
		return Result.OK(map);
	}

	@ApiOperation(value = "根据检验项目ID和检验单号查询对应检验明细", notes = "qis-根据检验项目ID和检验单号查询对应检验明细-分页列表查询")
	@GetMapping(value = "/getQuantitativeDetail")
	@DS("#header.his")
	public Result<List<QisMatdetailValue>> getQuantitativeDetail(@RequestParam Map<String, Object> map,
																 HttpServletRequest req) {
		List<QisMatdetailValue> resultMap = qisMatheadValueService.getQuantitativeDetail(map);
		return Result.OK(resultMap);
	}

	/**
	 * 添加
	 *
	 * @param qisMatheadValue
	 * @return
	 */
	@AutoLog(value = "qis-来料检测送检单数据表-添加")
	@ApiOperation(value = "qis-来料检测送检单数据表-添加", notes = "qis-来料检测送检单数据表-添加")
	@PostMapping(value = "/add")
	@DS("#header.line")
	public Result<String> add(@RequestBody QisMatheadValue qisMatheadValue) {
		qisMatheadValueService.addQisMatheadValue(qisMatheadValue);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param qisMatheadValue
	 * @return
	 */
	@AutoLog(value = "qis-来料检测送检单数据表-编辑")
	@ApiOperation(value = "qis-来料检测送检单数据表-编辑", notes = "qis-来料检测送检单数据表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
	@DS("#header.line")
	public Result<String> edit(@RequestBody QisMatheadValue qisMatheadValue) {
		qisMatheadValueService.edit(qisMatheadValue);
		return Result.OK("编辑成功!");
	}

	/**
	 * 根据送检单号生成检验明细单
	 *
	 * @param qisMatheadValue
	 * @return
	 */
	@AutoLog(value = "根据送检单号生成检验明细单")
	@ApiOperation(value = "根据送检单号生成检验明细单", notes = "根据送检单号生成检验明细单")
	@RequestMapping(value = "/submission", method = {RequestMethod.PUT, RequestMethod.POST})
	@DS("#header.line")
	public Result<String> qisMaInspectionSubmission(@RequestBody QisMatheadValue qisMatheadValue) {
		qisMatheadValueService.qisMaInspectionSubmission(qisMatheadValue);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "qis-来料检测送检单数据表-通过id删除")
	@ApiOperation(value = "qis-来料检测送检单数据表-通过id删除", notes = "qis-来料检测送检单数据表-通过id删除")
	@DeleteMapping(value = "/delete")
	@DS("#header.line")
	public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
		qisMatheadValueService.delMain(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "qis-来料检测送检单数据表-批量删除")
	@ApiOperation(value = "qis-来料检测送检单数据表-批量删除", notes = "qis-来料检测送检单数据表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@DS("#header.line")
	public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.qisMatheadValueService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 导出
	 *
	 * @return
	 */
	@RequestMapping(value = "/exportXls")
	@DS("#header.his")
	public ModelAndView exportXls(HttpServletRequest request, QisMatheadValue qisMatheadValue) {
		return super.exportXls(request, qisMatheadValue, QisMatheadValue.class, "qis-来料检测送检单数据表");
	}

	/**
	 * 导入
	 *
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@DS("#header.line")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, QisMatheadValue.class);
	}
	/*---------------------------------主表处理-end-------------------------------------*/


	/*--------------------------------子表处理-qis-来料检测结果明细表-begin----------------------------------------------*/

	/**
	 * 通过主表ID查询1
	 *
	 * @return
	 */
	//@AutoLog(value = "qis-来料检测结果明细表-通过主表ID查询")
	@ApiOperation(value = "qis-来料检测结果明细表-通过主表ID查询", notes = "qis-来料检测结果明细表-通过主表ID查询")
	@GetMapping(value = "/listQisMatdetailValueByMainId")
	@DS("#header.his")
	public Result<IPage<QisMatdetailValue>> listQisMatdetailValueByMainId(QisMatdetailValue qisMatdetailValue,
																		  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
																		  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
																		  HttpServletRequest req) {
		QueryWrapper<QisMatdetailValue> queryWrapper = QueryGenerator.initQueryWrapper(qisMatdetailValue, req.getParameterMap());
		Page<QisMatdetailValue> page = new Page<QisMatdetailValue>(pageNo, pageSize);
		IPage<QisMatdetailValue> pageList = qisMatdetailValueService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param qisMatdetailValue
	 * @return
	 */
	@AutoLog(value = "qis-来料检测结果明细表-添加")
	@ApiOperation(value = "qis-来料检测结果明细表-添加", notes = "qis-来料检测结果明细表-添加")
	@PostMapping(value = "/addQisMatdetailValue")
	@DS("#header.line")
	public Result<String> addQisMatdetailValue(@RequestBody QisMatdetailValue qisMatdetailValue) {
		qisMatdetailValueService.save(qisMatdetailValue);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param param
	 * @return
	 */
	@AutoLog(value = "qis-来料检测结果明细表-编辑")
	@ApiOperation(value = "qis-来料检测结果明细表-编辑", notes = "qis-来料检测结果明细表-编辑")
	@RequestMapping(value = "/editQisMatdetailValue")
	@DS("#header.line")
	public Result<String> editQisMatdetailValue(@RequestParam Map<String, String> param) {
		qisMatdetailValueService.edit(param);
		return Result.OK("编辑成功!");
	}

	/**
	 * 编辑
	 *
	 * @param qisMatheadValue
	 * @return
	 */
	@AutoLog(value = "qis-来料检测结果明细表-编辑")
	@ApiOperation(value = "qis-来料检测结果明细表-编辑", notes = "qis-来料检测结果明细表-编辑")
	@RequestMapping(value = "/verificationInspection")
	@DS("#header.line")
	public Result<String> verificationInspection(@RequestBody QisMatheadValue qisMatheadValue) {
		return qisMatheadValueService.verificationInspection(qisMatheadValue);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "qis-来料检测结果明细表-通过id删除")
	@ApiOperation(value = "qis-来料检测结果明细表-通过id删除", notes = "qis-来料检测结果明细表-通过id删除")
	@DeleteMapping(value = "/deleteQisMatdetailValue")
	@DS("#header.line")
	public Result<String> deleteQisMatdetailValue(@RequestParam(name = "id", required = true) String id) {
		qisMatdetailValueService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "qis-来料检测结果明细表-批量删除")
	@ApiOperation(value = "qis-来料检测结果明细表-批量删除", notes = "qis-来料检测结果明细表-批量删除")
	@DeleteMapping(value = "/deleteBatchQisMatdetailValue")
	@DS("#header.line")
	public Result<String> deleteBatchQisMatdetailValue(@RequestParam(name = "ids", required = true) String ids) {
		this.qisMatdetailValueService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 导出
	 *
	 * @return
	 */
	@RequestMapping(value = "/exportQisMatdetailValue")
	@DS("#header.his")
	public ModelAndView exportQisMatdetailValue(HttpServletRequest request, QisMatdetailValue qisMatdetailValue) {
		// Step.1 组装查询条件
		QueryWrapper<QisMatdetailValue> queryWrapper = QueryGenerator.initQueryWrapper(qisMatdetailValue, request.getParameterMap());
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
		List<QisMatdetailValue> pageList = qisMatdetailValueService.list(queryWrapper);
		List<QisMatdetailValue> exportList = null;

		// 过滤选中数据2
		String selections = request.getParameter("selections");
		if (oConvertUtils.isNotEmpty(selections)) {
			List<String> selectionList = Arrays.asList(selections.split(","));
			exportList = pageList.stream().filter(item -> selectionList.contains(item.getRecId())).collect(Collectors.toList());
		} else {
			exportList = pageList;
		}

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		//此处设置的filename无效,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.FILE_NAME, "qis-来料检测结果明细表");
		mv.addObject(NormalExcelConstants.CLASS, QisMatdetailValue.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("qis-来料检测结果明细表报表", "导出人:" + sysUser.getRealname(), "qis-来料检测结果明细表"));
		mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		return mv;
	}

	/**
	 * 导入
	 *
	 * @return
	 */
	@RequestMapping(value = "/importQisMatdetailValue/{mainId}")
	@DS("#header.line")
	public Result<?> importQisMatdetailValue(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			// 获取上传文件对象
			MultipartFile file = entity.getValue();
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {
				List<QisMatdetailValue> list = ExcelImportUtil.importExcel(file.getInputStream(), QisMatdetailValue.class, params);
				for (QisMatdetailValue temp : list) {
					temp.setHeadid(mainId);
				}
				long start = System.currentTimeMillis();
				qisMatdetailValueService.saveBatch(list);
				log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				return Result.OK("文件导入成功！数据行数：" + list.size());
			} catch (Exception e) {
				log.error(e.getMessage(), e);
				return Result.error("文件导入失败:" + e.getMessage());
			} finally {
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return Result.error("文件导入失败！");
	}

	/*--------------------------------子表处理-qis-来料检测结果明细表-end----------------------------------------------*/


}
